package com.ws.datasync.service.impl;

import com.ws.datasync.bean.Actual;
import com.ws.datasync.bean.Cluter;
import com.ws.datasync.bean.CluterNoder;
import com.ws.datasync.bean.HealthActual;
import com.ws.datasync.service.CluterHealthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class CluterHealthServiceImpl implements CluterHealthService {

    @Autowired
    private  Cluter cluter;




    @Override
    public List<HealthActual> queryHealthActual() {
         String querySql="show all slaves status";

         ///String querySql ="SHOW SLAVE STATUS";

         List<HealthActual> HealthActuals=new ArrayList<>();
        for(CluterNoder cluterNoder : cluter.getCluter()){
           DataSource dataSource= DataSourceBuilder.create().url(cluterNoder.getJdbcurl()).password(cluterNoder.getPassword()).username(cluterNoder.getUsername())
                    .build();
            JdbcTemplate jdbcTemplate= new  JdbcTemplate(dataSource);
            HealthActual healthActual=new HealthActual();
            healthActual.setName(cluterNoder.getName());
            healthActual.setJdbcurl(cluterNoder.getJdbcurl());
            List<Actual>  actuals=new ArrayList<Actual>();
            List<Map<String,Object>> result=jdbcTemplate.queryForList(querySql);
            for(Map<String,Object> slave: result){
                Actual slaveStatue=new Actual();
                slaveStatue.setConnecttionName(String.valueOf(slave.get("Connection_name")));
                slaveStatue.setMasterHost(String.valueOf(slave.get("Master_Host")));
                slaveStatue.setMasterLogFile(String.valueOf(slave.get("Master_Log_File")));
                slaveStatue.setReadMasterLogPos(String.valueOf(slave.get("Read_Master_Log_Pos")));
                slaveStatue.setLastErroNo(String.valueOf(slave.get("Last_Errno")));
                slaveStatue.setLastErro(String.valueOf(slave.get("Last_Error")));
                slaveStatue.setExecMasterLogPos(String.valueOf(slave.get("Exec_Master_Log_Pos")));
                slaveStatue.setGtidSlavePos(String.valueOf(slave.get("Gtid_Slave_Pos")));
                actuals.add(slaveStatue);
            }
            healthActual.setActuals(actuals);
            HealthActuals.add(healthActual);
        }



        return HealthActuals;
    }

    @Override
    public Map<String, List<String>> getMasterStatus() {
        Map<String,List<String>> result =new HashMap();
        String querysql="show  MASTER  STATUS";

        for(CluterNoder cluterNoder : cluter.getCluter()) {

            DataSource dataSource= DataSourceBuilder.create().url(cluterNoder.getJdbcurl()).password(cluterNoder.getPassword()).username(cluterNoder.getUsername())
                    .build();
            JdbcTemplate jdbcTemplate= new  JdbcTemplate(dataSource);
            List<Map<String,Object>> listMasterStatu= jdbcTemplate.queryForList(querysql);
            if(listMasterStatu.size()==0)continue;
            String position= listMasterStatu.get(0).get("Position")!=null?listMasterStatu.get(0).get("Position").toString():"0";
            String file=listMasterStatu.get(0).get("File")!=null?listMasterStatu.get(0).get("File").toString():"";
            String masterName=cluterNoder.getRegion()+cluterNoder.getSort();

            String template="change master '"+masterName+"' to  master_host='"+cluterNoder.getIp()+"',master_user='"+cluterNoder.getUsername()+"',master_password='"+cluterNoder.getPassword()+"',master_log_file='"+file+"',master_log_pos="+position;
            if(result.containsKey(cluterNoder.getRegion())){
                result.get(cluterNoder.getRegion()).add(template);
            }  else {
                List<String> templates=new ArrayList<>();
                templates.add(template);
                result.put(cluterNoder.getRegion(),templates);
            }
        }
        return  result;
    }

}